import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class BankCardManageGUI extends JFrame implements ActionListener {
    private JLabel libCardNo; // 声明"标签"，用于显示信息
    private JLabel libUid;
    private JLabel libName;
    private JLabel libPassword;
    private JLabel libBalance;
    private JLabel libAmount;
    private JLabel libDurationOfDeposit;
    private JLabel libPrompt;
    private JTextField txtCardNo; // 声明"文本框"，用于输入信息
    private JTextField txtUid;
    private JTextField txtName;
    private JTextField txtPassword;
    private JTextField txtBalance;
    private JTextField txtAmount;
    private ButtonGroup bgDurationOfDeposit = new ButtonGroup(); // 声明"按钮组"
    private JRadioButton jrbDurationOfDeposit1; // 声明"单选按钮"
    private JRadioButton jrbDurationOfDeposit2;
    private JRadioButton jrbDurationOfDeposit3;
    private JCheckBox jcbDeposit; // 声明"复选框"，办好卡时选择是否存款
    private JButton btnCreateBankCard; // 声明"按钮"，用于银行卡操作
    private JButton btnDeposit;
    private JButton btnDrawMoney;
    private JButton btnChangePwd;
    private JButton btnNewBankCard;
    private JButton btnSearch;
    private JButton btnDelete;
    private JButton btnReset;
    private JTable resultTable; // 声明"表格模型"
    private DefaultTableModel dModel; // 声明"表格"显示银行卡操作的结果信息

    public BankCardManageGUI() { // 自定义类的构造方法
        setTitle("银行卡管理系统"); // 设置窗体标题

        // 初始化"标签"
        libCardNo = new JLabel("银行卡号");
        libUid = new JLabel("身份证号");
        libName = new JLabel("用户姓名");
        libPassword = new JLabel("密码");
        libBalance = new JLabel("余额");
        libAmount = new JLabel("存/取款");
        libDurationOfDeposit = new JLabel("存款期限");
        libPrompt = new JLabel("操作信息提示");

        // 初始化"文本框"
        txtCardNo = new JTextField();
        txtUid = new JTextField();
        txtName = new JTextField();
        txtPassword = new JTextField();
        txtBalance = new JTextField();
        txtBalance.setEditable(false);
        txtAmount = new JTextField();

        // 初始化"单选按钮"
        jrbDurationOfDeposit1 = new JRadioButton("1年");
        jrbDurationOfDeposit2 = new JRadioButton("2年");
        jrbDurationOfDeposit3 = new JRadioButton("3年");
        bgDurationOfDeposit.add(jrbDurationOfDeposit1);
        bgDurationOfDeposit.add(jrbDurationOfDeposit2);
        bgDurationOfDeposit.add(jrbDurationOfDeposit3);

        // 初始化"复选框"
        jcbDeposit = new JCheckBox("办好卡时存款", true);

        // 初始化"按钮"
        btnCreateBankCard = new JButton("办好卡");
        btnDeposit = new JButton("存款");
        btnDrawMoney = new JButton("取款");
        btnChangePwd = new JButton("改密码");
        btnNewBankCard = new JButton("开空卡");
        btnSearch = new JButton("查询");
        btnDelete = new JButton("删除");
        btnReset = new JButton("清空");

        // 初始化表格
        String[] columnNames = {"卡号", "姓名", "身份证号", "密码", "余额"}; // 表格列名
        String[][] data0 = new String[0][0]; // 表格数据源，初始化为空白
        dModel = new DefaultTableModel(data0, columnNames); // 定义表格的默认数据模型
        resultTable = new JTable(dModel); // 用默认的数据模型新建一个表格对象
        resultTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); // 支持自动调整列宽
        resultTable.setGridColor(Color.blue); // 设置表格线条颜色
        resultTable.setRowHeight(25); // 设置表格行高
        resultTable.setSelectionForeground(Color.red); // 设置当前选中内容的颜色
        resultTable.setShowVerticalLines(false); // 不显示垂直分隔线
        JScrollPane jsp = new JScrollPane(resultTable); // 将表格放入带滚动条面板中

        // 设置窗体属性
        setLayout(null); // 布局管理器为null
        setBounds(300, 50, 600, 580); // 窗体在屏幕上的位置为{300,50}，宽度为600，高度580
        setVisible(true); // 窗体可见
        setResizable(false); // 窗口不可改动大小

        // 将各个组件添加到容器中
        add(libCardNo);
        add(libUid);
        add(libName);
        add(libPassword);
        add(libBalance);
        add(libAmount);
        add(libDurationOfDeposit);
        add(libPrompt);
        add(txtCardNo);
        add(txtUid);
        add(txtName);
        add(txtPassword);
        add(txtBalance);
        add(txtAmount);
        add(jrbDurationOfDeposit1);
        add(jrbDurationOfDeposit2);
        add(jrbDurationOfDeposit3);
        add(jcbDeposit);
        add(btnNewBankCard);
        add(btnCreateBankCard);
        add(btnDeposit);
        add(btnDrawMoney);
        add(btnChangePwd);
        add(btnDelete);
        add(btnSearch);
        add(btnReset);
        add(jsp);

        // 定位各个界面元素
        libCardNo.setBounds(50, 10, 100, 40);
        libUid.setBounds(300, 10, 100, 40);
        libName.setBounds(50, 60, 100, 40);
        libPassword.setBounds(300, 60, 100, 40);
        libBalance.setBounds(50, 100, 100, 40);
        libAmount.setBounds(300, 100, 100, 40);
        libDurationOfDeposit.setBounds(50, 160, 100, 40);
        txtCardNo.setBounds(120, 10, 120, 40);
        txtUid.setBounds(380, 10, 200, 40);
        txtName.setBounds(120, 60, 120, 40);
        txtPassword.setBounds(380, 60, 120, 40);
        txtBalance.setBounds(120, 110, 120, 40);
        txtAmount.setBounds(380, 110, 120, 40);
        jrbDurationOfDeposit1.setBounds(120, 160, 50, 40);
        jrbDurationOfDeposit2.setBounds(240, 160, 50, 40);
        jrbDurationOfDeposit3.setBounds(360, 160, 100, 40);
        jcbDeposit.setBounds(100, 190, 120, 40);
        btnCreateBankCard.setBounds(100, 230, 80, 40);
        btnDeposit.setBounds(190, 230, 80, 40);
        btnDrawMoney.setBounds(280, 230, 80, 40);
        btnChangePwd.setBounds(370, 230, 80, 40);
        btnNewBankCard.setBounds(100, 280, 80, 40);
        btnSearch.setBounds(190, 280, 80, 40);
        btnDelete.setBounds(280, 280, 80, 40);
        btnReset.setBounds(370, 280, 80, 40);
        jsp.setBounds(50, 380, 500, 150);
        libPrompt.setBounds(50, 330, 550, 40);
        libPrompt.setForeground(Color.red);

        // 为所有按钮添加事件监听
        btnCreateBankCard.addActionListener(this);
        btnDeposit.addActionListener(this);
        btnDrawMoney.addActionListener(this);
        btnChangePwd.addActionListener(this);
        btnNewBankCard.addActionListener(this);
        btnSearch.addActionListener(this);
        btnDelete.addActionListener(this);
        btnReset.addActionListener(this);

        // 设置窗体关闭操作
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        // 如果"办好卡"按钮被点击
        if (e.getSource() == btnCreateBankCard) {
            createBankCard(); // 调用创建银行卡的方法
        }
        // 如果"存款"按钮被点击
        else if (e.getSource() == btnDeposit) {
            depositMoney(); // 调用存款操作方法
        }
        // 如果"取款"按钮被点击
        else if (e.getSource() == btnDrawMoney) {
            drawMoney(); // 调用取款操作方法
        }
        // 如果"改密码"按钮被点击
        else if (e.getSource() == btnChangePwd) {
            changePassword(); // 调用修改密码方法
        }
        // 如果"开空卡"按钮被点击
        else if (e.getSource() == btnNewBankCard) {
            newBankCard(); // 调用新建空白银行卡方法
        }
        // 如果"查询"按钮被点击
        else if (e.getSource() == btnSearch) {
            searchCard(); // 调用查询银行卡信息方法
        }
        // 如果"删除"按钮被点击
        else if (e.getSource() == btnDelete) {
            deleteCard(); // 调用删除银行卡方法
        }
        // 如果"清空"按钮被点击
        else if (e.getSource() == btnReset) {
            resetForm(); // 调用重置表单方法
        }
    }

    private void createBankCard() {
        // 验证输入
        if (txtCardNo.getText().isEmpty() || txtUid.getText().isEmpty() ||
                txtName.getText().isEmpty() || txtPassword.getText().isEmpty()) {
            libPrompt.setText("请填写完整的银行卡信息！");
            return;
        }

        // 获取输入信息
        String cardNo = txtCardNo.getText();
        String uid = txtUid.getText();
        String name = txtName.getText();
        String password = txtPassword.getText();
        String balance = "0.00"; // 初始余额为0

        // 如果选择了"办好卡时存款"，则使用输入的金额作为初始余额
        if (jcbDeposit.isSelected() && !txtAmount.getText().isEmpty()) {
            try {
                double amount = Double.parseDouble(txtAmount.getText());
                balance = String.format("%.2f", amount);
            } catch (NumberFormatException ex) {
                libPrompt.setText("存款金额格式不正确！");
                return;
            }
        }



        // 添加到表格
        Object[] rowData = {cardNo, name, uid, password, balance};
        dModel.addRow(rowData);

        // 更新余额显示
        txtBalance.setText(balance);
        libPrompt.setText("银行卡创建成功！卡号: " + cardNo);
    }

    private void depositMoney() {
        if (txtCardNo.getText().isEmpty()) {
            libPrompt.setText("请先输入银行卡号！");
            return;
        }

        if (txtAmount.getText().isEmpty()) {
            libPrompt.setText("请输入存款金额！");
            return;
        }
        // 获取存款期限
        String duration = "无";
        if (jrbDurationOfDeposit1.isSelected()) {
            duration = "1年";
        } else if (jrbDurationOfDeposit2.isSelected()) {
            duration = "2年";
        } else if (jrbDurationOfDeposit3.isSelected()) {
            duration = "3年";
        }

        try {
            double amount = Double.parseDouble(txtAmount.getText());
            if (amount <= 0) {
                libPrompt.setText("存款金额必须大于0！");
                return;
            }

            // 更新余额
            double currentBalance = txtBalance.getText().isEmpty() ? 0 : Double.parseDouble(txtBalance.getText());
            double newBalance = currentBalance + amount;
            txtBalance.setText(String.format("%.2f", newBalance));

            // 更新表格中的余额
            updateBalanceInTable(txtCardNo.getText(), newBalance);

            libPrompt.setText("存款成功！当前余额: " + String.format("%.2f", newBalance)+ "，存款期限: " + duration);
        } catch (NumberFormatException ex) {
            libPrompt.setText("存款金额格式不正确！");
        }
    }

    private void drawMoney() {
        if (txtCardNo.getText().isEmpty()) {
            libPrompt.setText("请先输入银行卡号！");
            return;
        }

        if (txtAmount.getText().isEmpty()) {
            libPrompt.setText("请输入取款金额！");
            return;
        }

        try {
            double amount = Double.parseDouble(txtAmount.getText());
            if (amount <= 0) {
                libPrompt.setText("取款金额必须大于0！");
                return;
            }

            double currentBalance = txtBalance.getText().isEmpty() ? 0 : Double.parseDouble(txtBalance.getText());
            if (amount > currentBalance) {
                libPrompt.setText("余额不足！当前余额: " + String.format("%.2f", currentBalance));
                return;
            }

            // 更新余额
            double newBalance = currentBalance - amount;
            txtBalance.setText(String.format("%.2f", newBalance));

            // 更新表格中的余额
            updateBalanceInTable(txtCardNo.getText(), newBalance);

            libPrompt.setText("取款成功！当前余额: " + String.format("%.2f", newBalance));
        } catch (NumberFormatException ex) {
            libPrompt.setText("取款金额格式不正确！");
        }
    }

    private void changePassword() {
        if (txtCardNo.getText().isEmpty()) {
            libPrompt.setText("请先输入银行卡号！");
            return;
        }

        if (txtPassword.getText().isEmpty()) {
            libPrompt.setText("请输入新密码！");
            return;
        }

        String newPassword = txtPassword.getText();

        // 更新表格中的密码
        for (int i = 0; i < dModel.getRowCount(); i++) {
            String cardNo = (String) dModel.getValueAt(i, 0);
            if (cardNo.equals(txtCardNo.getText())) {
                dModel.setValueAt(newPassword, i, 3);
                libPrompt.setText("密码修改成功！");
                return;
            }
        }

        libPrompt.setText("未找到对应的银行卡！");
    }

    private void newBankCard() {
        resetForm();
        txtCardNo.setText("");
        txtUid.setText("");
        txtName.setText("");
        txtPassword.setText("");
        txtBalance.setText("");
        txtAmount.setText("");
        jrbDurationOfDeposit1.setSelected(true);
        jcbDeposit.setSelected(true);
        libPrompt.setText("准备创建新银行卡...");
    }

    private void searchCard() {
        if (txtCardNo.getText().isEmpty()) {
            libPrompt.setText("请输入要查询的银行卡号！");
            return;
        }

        String searchCardNo = txtCardNo.getText();

        for (int i = 0; i < dModel.getRowCount(); i++) {
            String cardNo = (String) dModel.getValueAt(i, 0);
            if (cardNo.equals(searchCardNo)) {
                // 显示查询结果
                txtUid.setText((String) dModel.getValueAt(i, 2));
                txtName.setText((String) dModel.getValueAt(i, 1));
                txtPassword.setText((String) dModel.getValueAt(i, 3));
                txtBalance.setText((String) dModel.getValueAt(i, 4));
                libPrompt.setText("查询成功！卡号: " + cardNo);
                return;
            }
        }

        libPrompt.setText("未找到卡号为 " + searchCardNo + " 的银行卡！");
    }

    private void deleteCard() {
        if (txtCardNo.getText().isEmpty()) {
            libPrompt.setText("请输入要删除的银行卡号！");
            return;
        }

        String deleteCardNo = txtCardNo.getText();

        for (int i = 0; i < dModel.getRowCount(); i++) {
            String cardNo = (String) dModel.getValueAt(i, 0);
            if (cardNo.equals(deleteCardNo)) {
                dModel.removeRow(i);
                resetForm();
                libPrompt.setText("银行卡 " + deleteCardNo + " 已删除！");
                return;
            }
        }

        libPrompt.setText("未找到卡号为 " + deleteCardNo + " 的银行卡！");
    }

    private void resetForm() {
        txtCardNo.setText("");
        txtUid.setText("");
        txtName.setText("");
        txtPassword.setText("");
        txtBalance.setText("");
        txtAmount.setText("");
        jrbDurationOfDeposit1.setSelected(true);
        jcbDeposit.setSelected(true);
        libPrompt.setText("表单已重置");
    }

    private void updateBalanceInTable(String cardNo, double newBalance) {
        for (int i = 0; i < dModel.getRowCount(); i++) {
            String currentCardNo = (String) dModel.getValueAt(i, 0);
            if (currentCardNo.equals(cardNo)) {
                dModel.setValueAt(String.format("%.2f", newBalance), i, 4);
                break;
            }
        }
    }

    public static void main(String args[]) {
        new BankCardManageGUI();
    }

}